package com.gxa.util;

import com.cloopen.rest.sdk.BodyType;
import com.cloopen.rest.sdk.CCPRestSmsSDK;
import com.gxa.config.SendEmailConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Random;
import java.util.Set;

@Component
public class SendEmail {

    @Autowired
    private SendEmailConfig sendEmailConfig;

    public Boolean sendEmail(String phone,String vCode){
        CCPRestSmsSDK sdk = new CCPRestSmsSDK();
        sdk.init(sendEmailConfig.getServerIp(), sendEmailConfig.getServerPort());
        sdk.setAccount(sendEmailConfig.getAccountSId(), sendEmailConfig.getAccountToken());
        sdk.setAppId(sendEmailConfig.getAppId());
        sdk.setBodyType(BodyType.Type_JSON);
        String to = phone;
        String templateId= sendEmailConfig.getTemplateId();
        String[] datas = {vCode,"1"};
        HashMap<String, Object> result = sdk.sendTemplateSMS(to,templateId,datas);
        if("000000".equals(result.get("statusCode"))){
            //正常返回输出data包体信息（map）
            HashMap<String,Object> data = (HashMap<String, Object>) result.get("data");
            Set<String> keySet = data.keySet();
            for(String key:keySet){
                Object object = data.get(key);
                System.out.println(key +" = "+object);
            }
            return true;
        }else{
            //异常返回输出错误码和错误信息
            return false;
        }
    }
    /**
     * 获取随机验证码
     * @return
     */
    public String getCode(){
        Random random = new Random();
        String vCode = "";
        for (int i = 0; i < 4; i++) {
            int rand = random.nextInt(10);
            vCode += rand;
        }
        return vCode;
    }
}
